package com.sqx.modules.app.service.impl;

import com.sqx.modules.app.dao.MsgDao;
import com.sqx.modules.app.entity.Msg;
import com.sqx.modules.app.service.PhoneVerificationCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lu shun zhi
 * {@code @date} 2024/11/14 16:50
 */
@Service
public class PhoneVerificationCodeServiceImpl implements PhoneVerificationCodeService {
    @Resource
    MsgDao msgDao;

    /**
     * 存在或者更新验证码
     *
     * @param phone 电话号码
     * @param code  验证
     */
    @Override
    public void updateOrAdd(String phone, String code) {
        // todo 添加验证码是否过期
        //  为什么不用redis？使用redis保存验证码时效，使用mysql保存发送记录
        Msg byPhone = msgDao.findByPhone(phone);
        if (byPhone != null) {
            byPhone.setCode(String.valueOf(code));
            byPhone.setPhone(phone);
            msgDao.updateById(byPhone);
        } else {
            Msg msg = new Msg();
            msg.setCode(String.valueOf(code));
            msg.setPhone(phone);
            msgDao.insert(msg);
        }
    }
}
